package com.bsg.upm.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.bsg.upm.constant.DictTypeConstants;
import com.bsg.upm.dto.ComboDto;
import com.bsg.upm.dto.SubComboDto;
import com.bsg.upm.entity.ComboEntity;
import com.bsg.upm.entity.DefinitionSubServArchEntity;
import com.bsg.upm.entity.DefinitionSubServUnitScaleEntity;
import com.bsg.upm.entity.SubComboEntity;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;

/**
 * 套餐管理业务处理类
 * 
 * @author HCK
 *
 */
@Service
public class ComboService extends BaseService {

	/**
	 * 获取套餐
	 * 
	 * @param definitionServCode
	 *            服务定义代码
	 * @return 操作结果
	 * @throws APIException
	 *             API异常处理类
	 */
	public RespJson list(String definitionServCode) throws APIException {
		try {
			JSONArray retJsonArray = new JSONArray();
			List<ComboEntity> combos = comboDao.listByDfServCode(definitionServCode);
			for (ComboEntity combo : combos) {
				ComboDto comboDto = buildShowDto(combo);
				retJsonArray.add(comboDto);
			}
			return RespJsonFactory.buildOK(retJsonArray);
		} catch (Exception e) {
			logger.error("套餐查询异常", e);
			throw new APIException("套餐查询异常:" + e.getMessage());
		}
	}

	/**
	 * 获取套餐
	 * 
	 * @param comboId
	 *            套餐编码
	 * @return 操作结果
	 * @throws APIException
	 *             API异常处理类
	 */
	public RespJson get(String comboId) throws APIException {
		try {
			ComboEntity combo = comboDao.get(comboId);
			if (combo == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "该套餐不存在");
			}
			ComboDto comboDto = buildShowDto(combo);
			return RespJsonFactory.buildOK(comboDto);
		} catch (Exception e) {
			logger.error("套餐获取异常", e);
			throw new APIException("套餐获取异常:" + e.getMessage());
		}
	}

	private ComboDto buildShowDto(ComboEntity combo) {
		ComboDto comboDto = new ComboDto();
		comboDto.setId(combo.getId());
		comboDto.setName(combo.getName());
		comboDto.setDescription(combo.getDescription());
		List<SubComboEntity> subCombos = combo.getSubCombos();
		List<SubComboDto> subComboDtos = new ArrayList<>(subCombos.size());
		comboDto.setSubCombos(subComboDtos);
		for (SubComboEntity subCombo : subCombos) {
			SubComboDto subComboDto = new SubComboDto();
			subComboDtos.add(subComboDto);

			subComboDto.setId(subCombo.getId());
			DefinitionSubServArchEntity arch = subCombo.getArch();
			subComboDto.setSubServType(arch.getDefinitionSubServ().getName());
			subComboDto.setArchId(arch.getId());
			subComboDto.setArchName(arch.getName());

			DefinitionSubServUnitScaleEntity scale = subCombo.getScale();
			subComboDto.setScaleId(scale.getId());
			subComboDto.setScaleName(scale.getName());

			subComboDto.setDataDirDeviceType(subCombo.getDataDirDeviceType());
			subComboDto.setDataDirDeviceTypeText(
					dictTypeCache.getTextFromCache(DictTypeConstants.DATA_DIR_TYPE, subCombo.getDataDirDeviceType()));
			subComboDto.setDataDirSize(subCombo.getDataDirSize());

			subComboDto.setLogDirDeviceType(subCombo.getLogDirDeviceType());
			subComboDto.setLogDirDeviceTypeText(
					dictTypeCache.getTextFromCache(DictTypeConstants.LOG_DIR_TYPE, subCombo.getLogDirDeviceType()));
			subComboDto.setLogDirSize(subCombo.getLogDirSize());

			subComboDto.setNetworkBandwidth(subCombo.getNetworkBandwidth());
			subComboDto.setMixed(subCombo.getMixed());
			subComboDto.setHighAvailable(subCombo.getHighAvailable());
		}
		return comboDto;
	}

}
